#ifndef _BLK_H_
#define _BLK_H_

#define NR_BLK_DEV  7

#define NR_REQUEST  32

struct request {
    int dev;                    // 使用的设备号，无请求则为-1
    int cmd;                    // 命令（READ或WRITE）
    int errors;                 // 操作时产生错误的次数
    unsigned long sector;       // 相对扇区号
    unsigned long nr_sectors;   // 读写扇区数
    char *buffer;               // 数据缓冲区
    struct buffer_head *bh;     // 缓冲区头指针
    struct request *next;       // 指向下一个请求项
};

#define IN_ORDER(s1, s2)    \
((s1)->cmd < (s2)->cmd || ((s1)->cmd == (s2)->cmd &&    \
((s1)->dev < (s2)->dev || ((s1)->dev == (s2)->dev &&    \
(s1)->sector < (s2)->sector))))

struct blk_dev_struct {
    void (*request_fn)(void);
    struct request *current_request;
};

extern struct blk_dev_struct blk_dev[NR_BLK_DEV];
extern struct request request[NR_REQUEST];
extern struct task_struct *wait_for_request;

#ifdef MAJOR_NR

#if (MAJOR_NR == 2)
/* floppy */
#define DEVICE_NAME "floppy"
#define DEVICE_INTR do_floppy
#define DEVICE_REQUEST do_fd_request
#define DEVICE_NR(device) ((device) & 3)
#define DEVICE_ON(device) floppy_on(DEVICE_NR(device))
#define DEVICE_OFF(device) floppy_off(DEVICE_NR(device))

#endif

#define REQUEST (blk_dev[MAJOR_NR].current_request)
#define REQUEST_DEV DEVICE_NR(REQUEST->dev)

#ifdef DEVICE_INTR
void (*DEVICE_INTR)(void) = NULL;
#endif
void (DEVICE_REQUEST)(void);

static inline void end_request()
{
    DEVICE_OFF(REQUEST->dev);   // 设置驱动器电动机停止的时间
    wake_up(&wait_for_request); // 唤醒等待的任务
    REQUEST->dev = -1;
    REQUEST = REQUEST->next;
}

#endif

#endif
